package com.lihan.GetSeatInLib.Configs;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.lihan.GetSeatInLib.Interceptors.JwtInterceptor;

@Configuration
public class WebConfig implements WebMvcConfigurer{

	@Autowired
	private JwtInterceptor interceptor;
	
	
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		
		registry.addInterceptor(interceptor).addPathPatterns("/**")
		.excludePathPatterns("/swagger-resources",
                "/swagger-resources/configuration/ui",
                "/swagger-resources/configuration/security",
                "/error",
                "/swagger-ui.html",
                "/webjars/**");
		
	}
	
//	@Bean
//	public JwtInterceptor getJwtInterceptor() {
//		return new JwtInterceptor();
//	}
	
	  @Override 
	  public void addResourceHandlers(ResourceHandlerRegistry registry) {
		  registry.addResourceHandler("/static/**").addResourceLocations(
		  "classpath:/static/");
		  
		  registry.addResourceHandler("swagger-ui.html")
		  .addResourceLocations("classpath:/META-INF/resources/");
		  
		  registry.addResourceHandler("/webjars/**")
		  .addResourceLocations("classpath:/META-INF/resources/webjars/");
	  }
	 
		
//	  @Bean public CorsFilter corsFilter() { 
//		  CorsConfiguration config = new
//		  CorsConfiguration(); // 设置允许跨域请求的域名 config.addAllowedOrigin("*"); // 是否允许证书
//		  //不再默认开启 // config.setAllowCredentials(true); // 设置允许的方法
//		  config.addAllowedMethod("*"); // 允许任何头 config.addAllowedHeader("*");
//		  config.addExposedHeader("token"); UrlBasedCorsConfigurationSource
//		  configSource = new UrlBasedCorsConfigurationSource();
//		  configSource.registerCorsConfiguration("/**", config); return new
//				  CorsFilter(configSource); 
//		  }
		 
	
}
